<!--
 Copyright (C) 2010 Choonghwan Lee (linjus.lee@gmail.com)

 This file is part of amy (http://unitcvt.codeplex.com/).

 amy is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 amy is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with amy. If not, see <http://www.gnu.org/licenses/>.
-->
<html>
<head>
	<script type="text/javascript" src="unit-definition.js"></script>
	<script type="text/javascript" src="unit-values.js"></script>
	<script type="text/javascript">
	var unitinfo;
	var category;
	var values;
	var output;

	function selectCategory(id) {
		unitinfo.setCurrentCategory(id);
		category = unitinfo.getCurrentCategory();
		values = category.instantiateUnitValues();

		var result = "<h2>Category " + id + "</h2>";
		output.innerHTML += result;
	}

	function evaluate(unitindex, value) {
		values[unitindex].value = value;

		category.convert(values[unitindex], values);
		var result = "<h3>Testing " + unitindex + " : " + value + "</h3>";
		for (var i = 0; i < values.length; ++i) {
			var value = values[i];
			result += "" + value.unit.name + ": " + value.value + "<br />";
		}

		output.innerHTML += result;
	}

	function test1() {
		output.innerHTML += "<h1>test1</h1>";

		// length
		selectCategory("length");
		if (true) {
			evaluate(5, "5'10\"");
			evaluate(5, "5'");
			evaluate(5, "10\"");
			evaluate(0, 0);
			evaluate(1, 1);
			evaluate(6, 1);
		}

		selectCategory("mass");
		if (true) {
			evaluate(0, 1);
			evaluate(1, 1);
			evaluate(2, 1);
			evaluate(3, 1);
		}

		selectCategory("temperature");
		if (true) {
			evaluate(1, 1);
			evaluate(0, 0);
			evaluate(1, 0);
			evaluate(2, 0);
		}
	}

	function isAggregateUnit(aggreunit, cur) {
		if (aggreunit == null)
			return false;

		for (var i = 0; i < aggreunit.length; ++i) {
			if (aggreunit[i] == cur)
				return true;
		}
		return false;
	}

	function testExhaustive(id, aggreunit) {
		output.innerHTML += "<h1>testExhaustive " + id + "</h1>";

		selectCategory(id);

		for (var i = 0; i < category.units.length; ++i) {
			if (isAggregateUnit(aggreunit, i)) continue;

			var testedvalues = [ 0, 1 ];
			for (var j = 0; j < testedvalues.length; ++j) {
				// assign the tested value to the i'-th unit
				var sourcevalue = testedvalues[j];
				output.innerHTML += "<h3>" + i + "-th unit := " + sourcevalue + "</h3>";
				values[i].value = sourcevalue;
				category.convert(values[i], values);

				// save converted values corresponding to 1 in i-th unit
				var converted = new Array();
				for (var k = 0; k < values.length; ++k) {
					converted.push(values[k].value);
					output.innerHTML += k + "-th unit: " + values[k].value + "<br />";
				}

				for (var k = 0; k < values.length; ++k) {
					if (isAggregateUnit(aggreunit, k)) continue;

					values[k].value = converted[k];
					var assigned = values[k].value;
					category.convert(values[k], values);
					var reconverted = values[i].value;
					output.innerHTML += k + "-th unit := " + assigned + " -> " + reconverted + "<br />";

					var diff = reconverted - sourcevalue;
					if (Math.abs(diff) > 0.0000001)
						throw "insignificant error: " + diff;
				}
			}
		}
	}

	function testAll() {
		unitinfo = new UnitInformation();
		unitinfo.initialize();
		output = document.getElementById("divOutput");

//		test1();

		try {
			testExhaustive("length", [ 4 ]);
			testExhaustive("area", null);
			testExhaustive("volume", null);
			testExhaustive("mass", null);
			testExhaustive("time", null);
			testExhaustive("speed", null);
			testExhaustive("pressure", null);
			testExhaustive("energy", null);
			testExhaustive("temperature", null);
			testExhaustive("information", null);
		}
		catch (e) {
			alert(e);
		}
	}
	</script>
</head>

<body onLoad="testAll()">
<div id="divOutput"></div>
</body>
</html>
